#include <iostream>
#include <string>

using namespace std;

#define InitSize 10
#define IncSize 5

template <typename T>
class SeqList
{
public:
    SeqList(int length = InitSize); // 构造函数，参数可以有默认值
    ~SeqList();                     // 析构函数
public:
    bool insert(int i, const T &e); // 在第i个位置上插入指定元素
    bool del(int i);                // 删除第i个位置的元素
    bool get(int i, T &e);          // 获得第i个位置的元素
    int indexOf(const T &e);        // 获得指定元素的第一次出现的位置
    void print();                   // 打印顺序表
    int len();                      // 获得顺序表的长度
    void reverse();                 // 反转顺序表
private:
    void expand(); // 顺序表存储空间的扩充
private:
    T *m_data;     // 顺序表的存储空间
    int m_length;  // 顺序表的长度
    int m_maxsize; // 顺序表的存储空间的大小
};

template <typename T>
SeqList<T>::SeqList(int length)
{
    m_data = new T[length]; // 初始化
    m_length = 0;
    m_maxsize = length;
}

template <typename T>
SeqList<T>::~SeqList()
{
    delete[] m_data;
}

template <typename T>
bool SeqList<T>::insert(int i, const T &e)
{
    if (m_length >= m_maxsize)
    {
        // expand();
        cout << "full" << endl;
    }
    if (i < 1 || i > m_length + 1)
    {
        cout << "error" << endl;
        return false;
    }
    for (int j = m_length; j >= i; j--)
    {
        m_data[j] = m_data[j - 1];
    }
    m_data[i - 1] = e;
    cout << "insert success" << endl;
    m_length++;
    return true;
}

template <typename T>
bool SeqList<T>::del(int i)
{
    if (m_length == 0)
    {
        cout << "empty" << endl;
        return false;
    }

    if (i < 1 || i > m_length)
    {
        cout << "error" << endl;
        return false;
    }

    cout << "delete success" << endl;

    for (int j = i; j < m_length; j++)
    {
        m_data[j - 1] = m_data[j];
    }
    m_length--;
    return true;
}

template <typename T>
bool SeqList<T>::get(int i, T &e)
{
    if (m_length == 0)
    {
        cout << "empty" << endl;
        return false;
    }

    if (i < 1 || i > m_length)
    {
        cout << "error" << endl;
        return false;
    }

    e = m_data[i - 1];
    return true;
}



template <typename T>
int SeqList<T>::indexOf(const T &e)
{
    for (int i = 0; i < m_length; i++)
    {
        if (m_data[i] == e)
        {
            cout << "found" << endl;
            return i + 1;
        }
    }
    cout << "not found" << endl;
    return -1;
}


template <typename T>
void SeqList<T>::print()
{
    for (int i = 0; i < m_length; i++)
    {
        // string s = i != m_length ? ",": " ";
        cout << m_data[i] << ",";
    }
    cout << endl;
}


template <typename T>
int SeqList<T>::len()
{
    return m_length;
}

template <typename T>
void SeqList<T>::reverse()
{
    for (int i = 0; i < m_length / 2; i++)
    {
        T temp = m_data[i];
        m_data[i] = m_data[m_length - i - 1];
        m_data[m_length - i - 1] = temp;
    }
}



int main()
{
    SeqList<int> seq(10);

    seq.insert(1, 15);
    seq.insert(2, 20);
    seq.insert(3, 25);
    seq.insert(4, 25);
    seq.insert(5, 25);
    seq.del(2);
    int eval = 0;
    seq.get(1, eval);
    cout << eval << endl;

    seq.indexOf(15);

    seq.print();


}